define(function () {

    var self, ggspItem,
        app = win.app,
        userInfo,
        pageParameters,
        addressInfo = {//客户
            GWAK001: '',// 客户编号
            GWAKL004: '',//客户名称
            GWAK028: '',// 区域县
            GWAK030: '',//电话
        },
        distributor = {//经销商
            GWAK001: '',// 客户编号
            GWAKL004: '',//客户名称
            GWAK028: '',// 区域县
            GWAK030: '',//电话
        },
        subData = {
            GWBHENT: '80',
            GWBHID: '',
            GWBHSEQ: '',
            GWBHNAME: '',//商机主题
            GWBH001: '1',//商机来源
            GWBH003: '',//拜访日期
            GWBH004: '',//起始时间
            GWBH005: '',//结束时间
            GWBH006: '',//拜访人员姓名
            GWBH007: '',//拜访人员职位
            GWBH008: '',//拜访人员角色
            GWBH009: '',//拜访地址
            GWBH010: '',//拜访目的
            GWBH011: '',//拜访内容
            GWBH012: '',//客户预算
            GWBH013: '',//预计购买时间
            GWBH014: '',//购买关键点
            GWBH015: '',//关键评估项
            GWBH016: '',//关联经销商编号
            GWBH017: '',//计划跟踪日期
            GWBH018: '',//下次拜访日期
            GWBH019: '',//下次拜访预计开始时间
            GWBH020: '',//下次拜访预计结束时间
            GWBH021: '',//下次拜访人员姓名
            GWBH022: '',//下次拜访人员职位
            GWBH023: '',//下次拜访人员角色
            GWBH024: '',//下次拜访地址
            GWBH025: '',//下次拜访目的
            GWBH026: '',//下次拜访内容
            GWBH027: '',//商机分类
            GWBH028: '',//商机阶段
            GWBH029: '',//商机评定原因说明
            GWBH030: '',//进入本阶段日期
            GWBH031: '',//成交几率
            GWBH032: '',//商机评定审核说明（主管回复）
            GWBH033: '',//备注
            GWBH034: '1',//维系方式
            GWBGL003: '',
            GWBHORIG: '',

            GWBHSJJE: 0,//商机金额
            _GWAK001: '',//客户
            _jxs: '',//经销商
        },
        sub_orders = [],
        sub_orders_old = [],
        g_sjjd = [],
        g_bffl = [], //拜访内容分类
        g_gwbh040 = []//领导批示
    ;

    function setCountAmount(c) {
        subData.gwbhsjje = c;
    }

    app.newModule({

        init: function () {
            self = this;
            ggspItem = app.cloneJSON(app.pageParameters['gwat171_01_page']);
            subData.GWBGL003 = ggspItem.GWBGL003;
            self._super({
                _scroll: '.mui-scroll-wrapper',
                _readyBefore: function () {
                    userInfo = app.userInfos.sysUser;
                    pageParameters = self._pageParameters;
                }
            }, win);
        },
        getInfo: function () {//获取表单信息
            app.ajax({
                xurl: "api/wx/gwat171/getGwbhInfo",
                xdata: {
                    GWBHENT: ggspItem.GWBHENT,
                    GWBHNAME: ggspItem.GWBHNAME,
                    GWBHID: ggspItem.GWBHID,
                    GWBHSEQ: ggspItem.GWBHSEQ
                },
                cb: function (result) {
                    if (result.ok) {
                        var data = result.data.data[0];

                        addressInfo.GWAK001 = data.GWAK001;
                        addressInfo.GWAK028 = data.GWAK028;
                        addressInfo.GWAK030 = data.GWAK030;
                        addressInfo.GWAKl004 = data.GWAKl004;

                        distributor.GWAK001 = data.GWAK0011;
                        distributor.GWAK028 = data.GWAK0281;
                        distributor.GWAK030 = data.GWAK0301;
                        distributor.GWAKl004 = data.GWAKl0041;

                        subData.GWBHENT = ggspItem.GWBHENT;
                        subData.GWBHID = ggspItem.GWBHID;
                        subData.GWBHSEQ = ggspItem.GWBHSEQ;
                        subData.GWBHNAME = data.GWBHNAME;//商机主题
                        //subData.GWBH001 = data.GWBH001;//商机来源
                        subData.GWBH003 = data.GWBH003;//拜访日期
                        subData.GWBH004 = data.GWBH004;//起始时间
                        subData.GWBH005 = data.GWBH005;//结束时间
                        subData.GWBH006 = data.GWBH006;//拜访人员姓名
                        subData.GWBH007 = data.GWBH007;//拜访人员职位
                        subData.GWBH008 = data.GWBH008;//拜访人员角色
                        subData.GWBH009 = data.GWBH009;//拜访地址
                        subData.GWBH010 = data.GWBH010;//拜访目的
                        subData.GWBH011 = data.GWBH011;//拜访内容
                        subData.GWBH012 = data.GWBH012;//客户预算
                        subData.GWBH013 = data.GWBH013;//预计购买时间
                        subData.GWBH014 = data.GWBH014;//购买关键点
                        subData.GWBH015 = data.GWBH015;//关键评估项
                        subData.GWBH016 = data.GWBH016;//关联经销商编号
                        subData.GWBH017 = data.GWBH017 || '';//计划跟踪日期
                        subData.GWBH018 = data.GWBH018 || '';//下次拜访日期
                        subData.GWBH019 = data.GWBH019 || '';//下次拜访预计开始时间
                        subData.GWBH020 = data.GWBH020 || '';//下次拜访预计结束时间
                        subData.GWBH021 = data.GWBH021;//下次拜访人员姓名
                        subData.GWBH022 = data.GWBH022;//下次拜访人员职位
                        subData.GWBH023 = data.GWBH023;//下次拜访人员角色
                        subData.GWBH024 = data.GWBH024;//下次拜访地址
                        subData.GWBH025 = data.GWBH025;//下次拜访目的
                        subData.GWBH026 = data.GWBH026;//下次拜访内容
                        subData.GWBH027 = data.GWBH027;//商机分类
                        subData.GWBH028 = data.GWBH028;//商机阶段
                        subData.GWBH029 = data.GWBH029;//商机评定原因说明
                        subData.GWBH030 = data.GWBH030 || '';//进入本阶段日期
                        subData.GWBH031 = data.GWBH031;//成交几率
                        subData.GWBH032 = data.GWBH032;//商机评定审核说明（主管回复）
                        subData.GWBH033 = data.GWBH033;//备注
                        subData.GWBHORIG = data.GWBHORIG;

                        if (data.GWBH040 && data.GWBH040.indexOf('[') != -1) {
                            g_GWBH040.pushArray(JSON.parse(data.GWBH040));
                        }

                        //subData.GWBH034 = data.GWBH034;//维系方式
                        if (data.GWBH034 == "1") {
                            subData.GWBH034 = "远程维系";

                        } else if (data.GWBH034 == "2") {
                            subData.GWBH034 = "上门拜访";

                        } else {
                            subData.GWBH034 = "宴请";
                        }

                        if (data.GWBH001 == "1") {
                            subData.GWBH001 = "自主挖掘";

                        } else if (data.GWBH001 == "2") {
                            subData.GWBH001 = "公司介绍";

                        } else {
                            subData.GWBH001 = "朋友介绍";
                        }
                        subData.GWBHSJJE = 0;//商机金额
                        subData._GWAK001 = '';//客户
                        subData._jxs = ''//经销商

                        self.getBffl();
                    }
                }
            });
        },
        getSjjd: function () {//商机阶段
            app.ajax({
                xurl: "api/wx/gwa/getSjjd",
                xdata: {
                    GWAK025: ''
                },
                cb: function (result) {
                    if (result.ok) {
                        var data = result.data;
                        g_sjjd.pushArray(data.data, true);
                        if (g_sjjd.length > 0) {
                            subData.GWBH028 = g_sjjd[0].GWAV001;
                        }
                    }
                }
            });
        },
        getSjje: function () {//商品明细
            app.ajax({
                xurl: "api/wx/gwat161/gwat161Spmx",
                xdata: {
                    GWBKNAME: ggspItem.GWBHNAME,
                    GWBHSEQ: ggspItem.GWBHSEQ
                },
                cb: function (result) {
                    if (result.ok) {
                        var c = 0;
                        result.data.data.forEach(function (v) {
                            c += (Number(v.GWBK004) * Number(v.GWBK003));
                        });
                        subData.GWBHSJJE = c;
                        sub_orders_old.pushArray(result.data.data, true);
                    }
                }
            });
        },
        getBffl: function () {//获取拜访记录内容分类
            app.ajax({
                xurl: "api/wx/gwat161/getGwbfListByzz",
                // xtime: '1d',
                cb: function (result) {
                    if (result.ok) {
                        g_bffl.pushArray(result.data.data, true);
                        if (subData.GWBH011 && subData.GWBH011.indexOf('{') != -1) {
                            var _gwbh011 = JSON.parse(subData.GWBH011);
                            g_bffl.forEach(function (v) {
                                v.msg = _gwbh011[v.GWBFL002];
                            });
                        }
                    }
                }
            });
        },
        _ready: function () {
            self.getInfo();
            new Vue({
                el: app.getByid(doc, 'gwat171Info'),
                data: {
                    sub: subData,
                    address: addressInfo,
                    distributor: distributor,
                    sjjd: g_sjjd,
                    bffls: g_bffl,
                    GWBH040S: g_gwbh040,
                    ORGID: userInfo.ORG_ID
                },
                methods: {
                    showSpmx: function () { // 商品明细
                        app.openPage({
                            src: 'hn-bfjltb/spmx.html',
                            args: {
                                orders: sub_orders,
                                bfjltb: subData,
                                ordersOld: sub_orders_old,
                                GWAK001: subData.GWBHID,
                                GWBHNAME: subData.GWBHNAME,
                                GWBHACTI: subData.GWBHACTI,
                                updateAmount: function (c) {
                                    setCountAmount(c);
                                }
                            }
                        });
                    },
                    showJzxx: function () { // 竞争信息
                        if (subData.GWBHNAME.trim() == '') {
                            app.toast('请选择商机主题');
                            return;
                        }
                        app.openPage({
                            src: 'hn-bfjltb/jzxx.html',
                            args: {
                                GWAK001: addressInfo.GWAK001,
                                GWBHNAME: subData.GWBHNAME,
                            }
                        });
                    },
                    showHxbf: function () { // 后续拜访
                        app.openPage({
                            src: 'hn-bfjltb/hxbf.html',
                            args: {
                                hxbfData: subData
                            }
                        });
                    },
                    submitData: function () {
                        subData.orders = sub_orders;
                        // var _gwbh011 = {};
                        // g_bffl.forEach(function (v) {
                        //     _gwbh011[v.gwbfl002] = v.msg;
                        // });
                        // subData.gwbh011 = app.jsonString(_gwbh011);

                        app.ajax({
                            xurl: "api/wx/gwat171/updateInfo",
                            xdata: subData,
                            loading: true,
                            rules: {
                                GWBH028: {nul: [false, '请选择商机阶段']},
                                GWBH030: {nul: [false, '请填写进入阶段日期']},
                                GWBH032: {nul: [false, '请输入主管回复']}
                            },
                            confirm: {
                                msg: '是否提交资料？'
                            },
                            cb: function (result) {
                                if (result.ok) {
                                    app.back(1);
                                }
                            }
                        });
                    }
                }
            });
        },
        _lazy: function () {
            this._super();

            self.getSjjd();
            self.getSjje();
        }
    });

});